<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<!--<title id="title">Server Info</title>-->
<script language="javascript" type="text/javascript" src="gamebrowser.js"></script>
<script language="javascript" type="text/javascript">
var ip = "127.0.0.1";
var port = 27015;

function onLoad()
{
	var query = document.location.search;

	if(query) {
		query = query.replace(/^\?/, '');
		var args = query.split('&', -1);
		for(var i=0;i<args.length;i++) {
			var v = args[i].split('=');
			if("ip" == v[0]) {
				ip = v[1];
			} else if("port" == v[0]) {
				port = v[1];
			}
		}
	}


	var server = fetchServerInfo(ip, port);

	var arr = new Array(
			"ip", "port", "name", "game", "maxplayers",
			"totalplayers", "updates", "lastUpdate",
			"secure", "dedicated", "os", "avarage",
			"latitude", "longitude", "gametype", "numplayers"
		);
	for(var i =0;i<arr.length;i++) {
		var tag = document.getElementById(arr[i]);
		if(tag) {
			tag.innerHTML = server ? server[arr[i]] : "server down";
		}
		var tags = document.getElementsByTagName(arr[i]);
		for(var c = 0;tags.length;c++) {
			tags[c].innerHTML = server ? server[arr[i]] : "server down";
		}
	}

	var links = document.getElementsByTagName("a");
	for(var i =0;i<links.length;i++) {
		if(links[i].href == 'ip:port') {
			links[i].href = 'steam://connect/'+ip+':'+port;
		}
	}
}

function fetchServerInfo(i, p)
{
	var obj;
	var arr;
	var url =  "http://lixo.servegame.com/cgi-bin/serverinfo?ip="+i+"&port="+p;
	if (XMLHttpRequest) {
		obj = new XMLHttpRequest();
	} else {
		obj = new ActiveXObject("MSXML2.XMLHTTP.3.0");
	}
	//alert(url);
	obj.open("GET", url, false);
	obj.send("");
	if(obj.status == 200) {
		var xml = obj.responseXML;
		//alert(obj.responseText);
		var list = xml.getElementsByTagName("server");
		if(!list) {
			alert("No server tag");
		}
		arr = new Array();
		var markerarr = new Array();
		for(var l=0; l<list.length; l++) {
			var attrs = list[l].childNodes;
			var i;
			var server = new Object();
			for(i=0;i<attrs.length;i++) {
				if(!attrs[i].tagName) {
					continue;
				}
				var value = elementValue(attrs[i]);
				server[attrs[i].tagName] = value;
			}
			arr.push(server);
		}
		return arr[0];
	} else {
		alert("Error fetching server info: "+obj.status);
	}
	return undef;
}
</script>
<style type="text/css">
v\:* {
	behavior:url(#default#VML);
}

th {
	text-align: left;
}

h1 {
	font-size: medium;
}

</style>

</head>
<body onload="onLoad();">
<h1 id="name"></h1>
<table boder="0">
<tr>
	<th>Address:</th>
	<td><span id="ip"></span>:<span id="port"></span></td>
</tr>
<tr>
	<th># Player:</th>
	<td id="numplayers"></td>
</tr>
<tr>
	<th>Maxplayers:</th>
	<td id="maxplayers"></td>
</tr>
<tr>
	<th>Game Type:</th>
	<td id="gametype"></td>
</tr>
<tr>
	<th>Avg. Player Count:</th>
	<td id="avarage"></td>
</tr>
<tr>
	<th>Secure:</th>
	<td id="secure"></td>
</tr>
<tr>
	<th>Dedicated:</th>
	<td id="dedicated"></td>
</tr>
<tr>
	<th>OS:</th>
	<td id="os"></td>
</tr>
<tr>
	<th>Last Update:</th>
	<td id="lastUpdate"></td>
</tr>
</table>
<p><a href="ip:port">Join Game</a><p>
</body>
</html>
